class Node(object):
    def __init__(self, name, type='dir'):
        self.name = name
        # dir or file
        self.type = type
        self.children = []
        self.parent = None

    def __repr__(self):
        return self.name


class FileSystemTree(object):
    def __init__(self):
        self.root = Node('/')
        self.now = self.root

    def mkdir(self, name):
        # name需要以/结尾
        if name[-1] != '/':
            name += '/'
        node = Node(name)
        self.now.children.append(node)
        node.parent = self.now

    # 显示全部的子节点
    def ls(self):
        return self.now.children

    # 显示目前处于的位置
    def show_position(self):
        ls = []
        ls.append(self.now.name)
        if self.now.parent is None:
            print(self.now.name)
        else:
            node = self.now.parent
            if node is not None:
                ls.append(node.name)
            else:
                print('/')
                return
            while node:
                node = node.parent
                if node is not None:
                    ls.append(node.name)
            print(''.join(ls[::-1]))

    def cd(self, name):
        if name[-1] != '/':
            name += '/'
        if name == '../':
            if self.now.parent is None:
                print('已经在根目录下了，无法再退')
            else:
                self.now = self.now.parent
            return
        for child in self.now.children:
            if child.name == name:
                self.now = child
                return
        else:
            raise ValueError('目录不存在')


tree = FileSystemTree()
tree.mkdir('c:')
tree.mkdir('d:')
tree.mkdir('e:')
# print(tree.ls())
tree.cd('c:')
tree.mkdir('系统文件:')
tree.cd('系统文件:')
tree.mkdir('电影:')
tree.cd('电影:')
print(tree.ls())
tree.show_position()
tree.cd('..')
print(tree.ls())
tree.cd('..')
tree.cd('..')
print(tree.ls())

# n = Node('hello')
# n1 = Node('world')
# n.children.append(n1)
# print(n.children[0].name)
